using System;
using ClientTools.Library.DataTypes.Stats;
using umbraco.BusinessLogic;
using umbraco.DataLayer;

namespace ClientTools.Library.Manager
{
    public static class StatsManager
    {

        private static ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        public static StatsInfo DeleteStatsByDate(DateTime date)
        {
            return new StatsInfo
                       {
                           Entries =
                               SqlHelper.ExecuteNonQuery(
                               "DELETE FROM umbracoStatEntry WHERE SessionID IN (SELECT ID FROM umbracoStatSession WHERE SessionStart < @date)",
                               SqlHelper.CreateParameter("@date", date)),
                           Sessions =
                               SqlHelper.ExecuteNonQuery(
                               "DELETE FROM umbracoStatSession WHERE SessionStart < @date",
                               SqlHelper.CreateParameter("@date", date))
                       };
        }

        public static StatsInfo GetStatsInfo()
        {
            // initialize the returning object
            return new StatsInfo
                       {
                           Sessions = SqlHelper.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoStatSession"),
                           Entries = SqlHelper.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoStatEntry")
                       };
        }
    }
}